package admin

import (
	"context"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/module/admin/resourceLogic"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/utils/database"
	"grpc.getaway.adminspro.padmins.com/pkg/businessCommonQuery"
	"grpc.getaway.adminspro.padmins.com/pkg/config/businessError"
	"grpc.getaway.adminspro.padmins.com/pkg/libs/utils"
	pbAdmin "grpc.getaway.adminspro.padmins.com/pkg/proto/admin"
	"strings"
)

func (rpc *admin) AdminResourceDetail(ctx context.Context, pb *pbAdmin.AdminResourceDetailReq) (*pbAdmin.AdminResourceDetailResp, error) {
	Db := database.Db.GetDefaultDb(ctx)

	row, dbErr := Db.Find("select * from admin_resource where id=?", pb.Id)

	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}

	if row == nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.NotFound)
	}

	if row["resource"].(string) == "" {
		row["resource"] = strings.ReplaceAll(row["path"].(string), "/", ".")
	}

	businessCommonQuery.ListFormat(resourceLogic.List(), row)

	resources, dbErr := Db.Query("select *,title as name from admin_resource")

	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}

	t := utils.NewTree()
	resources = t.Init(resources).GetTreeArray(int64(0))

	titles := []string{}

	for _, title := range utils.ArrayColumn(t.GetParents(row["id"]), "title") {
		titles = append(titles, title.(string))
	}

	resp := &pbAdmin.AdminResourceDetailResp{
		Row: &pbAdmin.AdminResourceDetailResp_AdminResource{
			Id:         row["id"].(int64),
			Pid:        row["pid"].(int64),
			Type:       int32(row["type"].(int64)),
			Path:       row["path"].(string),
			Resource:   row["resource"].(string),
			Title:      row["title"].(string),
			Icon:       row["icon"].(string),
			Remark:     row["remark"].(string),
			Badge:      row["badge"].(string),
			Weigh:      row["weigh"].(int64),
			Status:     int32(row["status"].(int64)),
			CreateTime: row["create_time"].(string),
			UpdateTime: row["update_time"].(string),
			StatusText: row["status_text"].(string),
			Name:       row["title"].(string),
			TypeText:   row["type_text"].(string),
			PidText:    utils.Implode("/", titles),
		},
		Menus: []*pbAdmin.AdminResourceDetailResp_AdminResourceMenu{},
	}

	ids := t.GetChildrenIds(row["id"], true)
	resources = t.GetTreeList(resources, "name")
	for _, resource := range resources {
		if resource["type"] == int64(1) {
			resp.Menus = append(resp.Menus, &pbAdmin.AdminResourceDetailResp_AdminResourceMenu{
				Id:         resource["id"].(int64),
				Pid:        resource["pid"].(int64),
				Type:       int32(resource["type"].(int64)),
				Path:       resource["path"].(string),
				Resource:   resource["resource"].(string),
				Title:      resource["title"].(string),
				Icon:       resource["icon"].(string),
				Remark:     resource["remark"].(string),
				Badge:      resource["badge"].(string),
				Weigh:      resource["weigh"].(int64),
				Status:     int32(resource["status"].(int64)),
				CreateTime: resource["create_time"].(string),
				UpdateTime: resource["update_time"].(string),
				Name:       resource["name"].(string),
				Disabled:   utils.InArray(resource["id"], ids),
			})
		}
	}
	return resp, nil
}
